package com.jiutong.meipenggang.http;

import com.jiutong.meipenggang.utils.EncryptUtil;

import java.util.Map;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 为所有请求添加观察者以及回调
 * Created by suxi on 2017/5/22.
 */

public class SubscribeHandler {
    /**
     * @param o
     * @param s
     * @param <T>
     */
    public <T> void toSubscribe(Observable<T> o, Subscriber<T> s) {
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s);
    }

    /**
     * 添加共同参数 SHA1加密
     *
     * @param fields
     */
    public void handleFields(Map<String, Object> fields) {
//        fields.put("appKey", "00001");
//        fields.put("v", "1.0");
        StringBuffer strA = new StringBuffer(EncryptUtil.getOrderByLexicographic(fields));
        StringBuffer stringSignTemp = strA.append("fixed.secret");
//        String sessionId = SpConfig.getInstance().getString(Constants.SESSIONID_STRING);
//        if (!TextUtils.isEmpty(sessionId))
//            fields.put(Constants.SESSIONID_STRING, sessionId);
        String md5 = null;
        try {
            md5 = EncryptUtil.encryptByMD5(stringSignTemp.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        fields.put("sign", md5);

    }

}
